<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>
    <base href="<%=basePath%>">
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <title>扫码加入公司</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <script type="text/javascript" src="static/js/jquery-1.11.0.js"></script>
</head>
<html>

<body>
<input id="scanCompanyId" value="${companyId}" type="hidden"/
</body>
</html>

<script>
    let ua = navigator.userAgent.toLowerCase();
    let isWechat = ua.indexOf('micromessenger') !== -1;

    if (!isWechat) {
        window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=888"
    }

    $(function () {
        // 自动加入扫描的公司
        let companyId = $("#scanCompanyId").val();

        alert('您扫描的公司ID为：' + companyId);

        alert('您已成功加入该公司');

        weixinClosePage();
    });

    function getUrlSearch(name) {
        // 未传参，返回空
        if (!name) return null;

        // 查询参数：先通过search取值，如果取不到就通过hash来取
        let after = window.location.search;
        after = after.substr(1) || window.location.hash.split('?')[1];

        // 地址栏URL没有查询参数，返回空
        if (!after) return null;

        // 如果查询参数中没有"name"，返回空
        if (after.indexOf(name) === -1) return null;

        let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');

        // 当地址栏参数存在中文时，需要解码，不然会乱码
        let r = decodeURI(after).match(reg);

        // 如果url中"name"没有值，返回空
        if (!r) return null;

        return r[2];
    }

    function weixinClosePage() {
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', weixin_ClosePage, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', weixin_ClosePage);
                document.attachEvent('onWeixinJSBridgeReady', weixin_ClosePage);
            }
        } else {
            weixin_ClosePage();
        }
    }

    function weixin_ClosePage() {
        WeixinJSBridge.call('closeWindow');
    }

</script>